﻿// Copyright (c) 2017 GlassGrass

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;





namespace GlassGrass.Unity.GuiFramework
	{

	partial class ContainerComponent
		{

		/// <summary>Collection of GuiElement for children collection.</summary>
		internal class GuiElementCollection : Collection<GuiElement>
			{

			private readonly ContainerComponent _parent;

			internal GuiElementCollection(ContainerComponent parent)
				{
				_parent = parent;
				}


			/// <summary></summary>
			/// <param name="index"></param>
			/// <param name="element"></param>
			protected override void InsertItem(int index, GuiElement element)
				{
				base.InsertItem(index, element);

				_parent.ChildAdded.OnNext(element);
				}


			/// <summary></summary>
			/// <param name="index"></param>
			/// <param name="newElement"></param>
			protected override void SetItem(int index, GuiElement newElement)
				{
				var oldElement = this[index];
				base.SetItem(index, newElement);

				_parent.ChildRemoved.OnNext(oldElement);
				_parent.ChildAdded.OnNext(newElement);
				}


			/// <summary></summary>
			/// <param name="index"></param>
			protected override void RemoveItem(int index)
				{
				var oldElement = this[index];
				base.RemoveItem(index);

				_parent.ChildRemoved.OnNext(oldElement);
				}


			/// <summary></summary>
			protected override void ClearItems()
				{
				var oldElements = new List<GuiElement>(this);
				base.ClearItems();

				foreach (var oldElement in oldElements)
					_parent.ChildRemoved.OnNext(oldElement);
				}

			}

		}

	}